package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import java.sql.SQLException;
import java.util.ArrayList;

import net.miginfocom.swing.MigLayout;
import pracownik.Pracownik;
import zadanie.Zadanie;

public class PracownikPokaz extends JFrame{
    private int id;
    private Pracownik worker;
    private MigLayout layout;
    
    private JLabel ImieLabel;
    private JLabel ImieValue;
    private JLabel NazwiskoLabel;
    private JLabel NazwiskoValue;
    private JLabel SpecjalizacjaLabel;
    private JLabel SpecjalizacjaValue;
    private JLabel StanowiskoLabel;
    private JLabel StanowiskoValue;
    private JLabel PunktyLabel;
    private JLabel PunktyValue;
    private JTabbedPane HarmonogramPanel;
    private JPanel panel;
    
    
    public PracownikPokaz(int id) throws SQLException
    {
        worker = Pracownik.getPracownikById(id);
        this.setTitle(worker.pokazImie()+ " " + worker.pokazNazwisko());
        layout = new MigLayout("", "[][]", "[][][][]");
        getContentPane().setLayout(layout);
        initComponents();
        
        this.setVisible(true);
        this.setSize(700, 450);
        this.setLocation(100, 100);
        this.setResizable(false);
        this.setAlwaysOnTop(true);
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);  
        this.loadTasks();
        
    }
    
    
    private void initComponents()
    {      
        
        ImieLabel = new JLabel("Imie: ");
        ImieValue = new JLabel(worker.pokazImie());
        ImieLabel.setFont(new Font(("Tahoma"), Font.BOLD, 15));
        ImieValue.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        getContentPane().add(ImieLabel, "cell 0 1");
        getContentPane().add(ImieValue, "cell 1 1");

        
        NazwiskoLabel = new JLabel("Nazwisko: ");
        NazwiskoValue = new JLabel(worker.pokazNazwisko());
        NazwiskoLabel.setFont(new Font(("Tahoma"), Font.BOLD, 15));
        NazwiskoValue.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        getContentPane().add(NazwiskoLabel, "cell 0 2");
        getContentPane().add(NazwiskoValue, "cell 1 2"); 
        
        
        
        StanowiskoLabel = new JLabel("Stanowisko: ");
        getContentPane().add(StanowiskoLabel, "cell 0 5"); 
        int idStanowiska = worker.pokazStanowisko();
        if(idStanowiska == 1)
        {
            StanowiskoValue = new JLabel("pracownik");
            getContentPane().add(StanowiskoValue, "cell 1 5"); 
            HarmonogramPanel = new JTabbedPane();
            panel = new JPanel();
            panel.setMinimumSize(new Dimension(580,580));
            HarmonogramPanel.add(new JScrollPane(panel), "Harmonogram");
            getContentPane().add(HarmonogramPanel, "cell 0 6 2 1"); 
            HarmonogramPanel.setSize(560, 400);
        }
        else if(idStanowiska == 2)
        {
            StanowiskoValue = new JLabel("kierownik");
            getContentPane().add(StanowiskoValue, "cell 1 5"); 
        }        
        StanowiskoLabel.setFont(new Font(("Tahoma"), Font.BOLD, 15));
        StanowiskoValue.setFont(new Font(("Tahoma"), Font.PLAIN, 15));
        
        
       
    }
    
    
    private void loadTasks()
    {
        ArrayList<Zadanie> Harm = this.worker.pokazHarmonogram();
        if(Harm.size()== 0)
        {
            panel.remove(HarmonogramPanel);
            HarmonogramPanel.setVisible(false);
            System.out.println("brak harm");
        }
        else
        {
            this.panel.setLayout(new MigLayout());
            for(int i=0; i<Harm.size(); i++)
            {
                System.out.println("pobrano task "+i);
                this.panel.add(createTaskRecord(Harm.get(i)),"wrap 5px"); 
            }
        }
    }
    
    private JPanel createTaskRecord(Zadanie z)
    {
       JPanel record = new JPanel();
       if(z.pokazStatus()==2)
       {
           record.setBackground(new Color(255,246,143));
       }
       else if(z.pokazStatus()==1)
       {
           record.setBackground(new Color(255,255,255));
       }
       else if(z.pokazStatus()==3)
       {
           record.setBackground(new Color(0,255,127));
       }
       else if(z.pokazStatus()==5)
       {
           record.setBackground(new Color(255,255,255));
           record.setBorder(BorderFactory.createLineBorder(Color.red));
       }
       else
       {
           record.setBackground(new Color(255,255,255)); 
       }
       record.setLayout(new MigLayout());
       record.setMinimumSize(new Dimension(612,30));
       final JLabel idLabel = new JLabel(Integer.toString(z.pokazId()));
       idLabel.setFont(new Font(("TimesRoman"), Font.BOLD, 12));
       idLabel.setMinimumSize(new Dimension(30,15));
       final JLabel titleLabel = new JLabel(z.pokazTytul());
       titleLabel.setMaximumSize(new Dimension(100,15));
       titleLabel.setMinimumSize(new Dimension(110,15));
       titleLabel.setBorder(BorderFactory.createLineBorder(Color.gray));
       final JLabel terminLabel = new JLabel(z.pokazTermin().toString());
       terminLabel.setMinimumSize(new Dimension(57,15));

       record.add(idLabel);
       record.add(titleLabel);
       record.add(terminLabel);

       return  record;
    }
}
